<template>
	<view class="forget">
		<view>
			<navbar title='忘记密码' :is-child="true" />
		</view>
		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">邮箱无法使用？请联系客服</view>
				<wInput
					v-model="forgetForm.email"
					type="text"
					maxlength="20"
					placeholder="请输入邮箱"
				></wInput>
				<wInput
					v-model="forgetForm.password"
					type="password"
					maxlength="20"
					placeholder="请输入新密码"
					isShowPass
				></wInput>
				
				<wInput
					v-model="forgetForm.code"
					type="number"
					maxlength="6"
					placeholder="验证码"
					
					isShowCode
					codeText="获取验证码"
					setTime="30"
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				class="wbutton"
				text="重置密码"
				:rotate="isRotate" 
				@click.native="startRePass()"
			></wButton>

		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '@/components/watch-login/watch-input.vue' //input
	import wButton from '@/components/watch-login/watch-button.vue' //button
	
	import { getEmailCode, forget } from '@/apis/user.js'
	
	export default {
		data() {
			return {
				phoneData: "", //电话
				passData: "", //密码
				verCode:"", //验证码
				isRotate: false, //是否加载旋转
				forgetForm: {
					email: '',
					password: '',
					uuid: '',
					code: '',
				},
			}
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			_this= this;
		},
		methods: {
			getVerCode(){
				console.log("获取验证码")
				if (_this.forgetForm.email.length ===  0) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '邮箱不能为空'
				    });
				    return false;
				}
				this.$refs.runCode.$emit('runCode'); //触发倒计时
				// _this.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
				getEmailCode({email: _this.forgetForm.email}).then( res => {
					uni.showToast({
					    icon: 'success',
						position: 'bottom',
					    title: '验证码发送成功'
					});
					this.forgetForm.uuid = res.data.uuid;
				}).catch( e => {
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '验证码发送失败'
					});
				});
			},
			startRePass() {
				//重置密码
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (_this.forgetForm.email.length ===  0) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '邮箱不能为空'
				    });
				    return false;
				}
				if (_this.forgetForm.password.length ===  0) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '密码不能为空'
				    });
				    return false;
				}
				if (_this.forgetForm.code.length ===  0) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不能为空'
				    });
				    return false;
				}
				
				forget(_this.forgetForm).then( res => {
					uni.showToast({
						icon: 'success',
						position: 'bottom',
						title: '密码修改成功'
					});
					setTimeout(function(){
						uni.navigateTo({
							url: '/pages/me/login/login'
						})
					}, 1000);
				}).catch((e) => {
					if(e.code != undefined) {
						let message = e.message;
						uni.showToast({
							icon: 'error',
							position: 'bottom',
							title: message || '密码修改失败'
						});
					}
				}).finally( r => {
					_this.isRotate=false;
				})
			}
		}
	}
</script>

<style>
	@import url("@/components/watch-login/css/icon.css");
	@import url("@/common/search.css");
</style>

